/**
 * 选择本地图片
 */
export function chooseLocalImg() {
  return new Promise((resolve, reject) => {
    uni.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: function (res) {
        resolve(res);
      },
      fail: function (err) {
        reject(err);
      },
    });
  });
}


/**
 * 选择本地图片并上传
 */
export function chooseLocalImgAndUpload() {
  return new Promise((resolve, reject) => {
    uni.chooseMedia({
      count: 1,
      sizeType:  ['compressed'],
      mediaType: ['image'],
      success: function (chooseImageRes) {
        // console.log(chooseImageRes)
        const tempFilePaths = chooseImageRes.tempFiles[0].tempFilePath;
        uni.showLoading({
          title: '上传中...',
          mask: true,
        })
        uni.uploadFile({
          url: import.meta.env.VITE_BASE_URL+ '/api/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: (uploadFileRes) => {
            const data = JSON.parse(uploadFileRes.data);
            if(data.link) {
              resolve(data.link);
            }else {
              uni.hideLoading();
              uni.showToast({
                title: '上传失败',
                icon: 'none',
              });
              reject(null)
            }
          },
          complete: function () {
            uni.hideLoading();
          }
        });
      },
      fail: function (err) {
        reject(null);
      },
    });
  });
}
